package com.zzzyh.forumsystemclient.services.impl;

import com.zzzyh.forumsystemclient.dao.ResourceMapper;
import com.zzzyh.forumsystemclient.dao.UserMapper;
import com.zzzyh.forumsystemclient.model.Resource;
import com.zzzyh.forumsystemclient.model.User;
import com.zzzyh.forumsystemclient.services.IResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResourceImpl implements IResourceService {
    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Resource> listByCategory(Integer pageNum, Integer pageSize, Integer category) {
        List<Resource> resources = resourceMapper.listByCategory((pageNum - 1) * pageSize, pageSize, category);
        for (Resource resource : resources){
            User user = userMapper.selectByPrimaryKey(resource.getUserId());
            resource.setNickname(user.getNickname());
            resource.setAvatar(user.getAvatar());
        }
        return resources;
    }

    @Override
    public Integer totalByCategoryId(Integer category) {
        return resourceMapper.totalByCategoryId(category);
    }

    @Override
    public void add(Resource resource) {
        resourceMapper.insertSelective( resource);
    }

    @Override
    public void increaseDownload(Integer resourceId) {
        resourceMapper.increaseDownload(resourceId);
    }

    @Override
    public void increaseView(Integer resourceId) {
        resourceMapper.increaseView(resourceId);
    }

    @Override
    public List<Resource> listByCategoryName(Integer pageNum, Integer pageSize, Integer category, String name) {
        List<Resource> resources = resourceMapper.listByCategoryAndName((pageNum - 1) * pageSize, pageSize, category,name);
        for (Resource resource : resources){
            User user = userMapper.selectByPrimaryKey(resource.getUserId());
            resource.setNickname(user.getNickname());
            resource.setAvatar(user.getAvatar());
        }
        return resources;
    }

    @Override
    public Integer totalByCategoryIdAndName(Integer category, String name) {
        return resourceMapper.totalByCategoryIdAndName(category, name);
    }


}
